package io.renren.controller;


import io.renren.entity.*;
import io.renren.service.*;
import io.renren.common.utils.R;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;


/**
 * 我的界面
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/api")
@SuppressWarnings("all")
public class MyMineController {
	@Autowired
	CoupondetailService coupondetailService;

	@Autowired
	CouponService couponService;

	@Autowired
	ReplenishmentService replenishmentService;

	@Autowired
	ReplenishmentitemService replenishmentitemService;

	@Autowired
	GoodshelvesService goodshelvesService;

	@Autowired
	OrdersService ordersService;

	@Autowired
	UsersService usersService;

	/**
	 * 用户信息
	 * @param userId
	 * @return
	 */
	@RequestMapping("/user")
	@ResponseBody
	public R user(@RequestParam("userid")Integer userid) {
		if(userid ==null){
			return R.error("用户id不能为空");
		}else{
			UsersVOEntity user = usersService.getById(userid);
			if(user!=null){
				return R.ok().put("headimg",user.getHeadimg()).put("username",user.getUsername());
			}else{
				return R.error("找不到该用户");
			}

		}

	}

	/**
	 * 我的优惠卷
	 * @param userid 用户id
	 * @return
	 */
	@RequestMapping("/coupon")
	@ResponseBody
	public R coupon(@RequestParam("userid")Integer userid) {
		JSONArray array = new JSONArray();
		if(userid ==null){
			return R.error("购买人id不能为空");
		}else{
			List<CoupondetailEntity> couponlist = coupondetailService.queryList(userid);
			if(couponlist!=null && couponlist.size()>0){
				for(CoupondetailEntity ce:couponlist){
					JSONObject json = new JSONObject();
					CouponEntity coupon = couponService.getById(ce.getCouponid());
					String conditions = "";
					if(coupon.getConditions() == 0){
						conditions = "无门槛";
					}else{
						conditions = "满"+coupon.getConditions()+"元可用";
					}
					json.put("name",coupon.getName());
					json.put("denomination",coupon.getDenomination());
					json.put("conditions",conditions);
					String time = "";
					if(coupon.getEffectivedays() == 0){
						time = coupon.getValiditystar() +"-"+ coupon.getValidityend();
					}else{
						time = "领取当日"+coupon.getEffectivedays()+"天内有效";
					}
					json.put("time",time);
					json.put("statu",coupon.getStatu());
					array.add(json);
				}
			}
		}

		return R.ok().put("coupons", array);
	}

	/**
	 * 我的补货单
	 * @param userid 用户id
	 * @param statu 补货单状态 0 已提交 1 已处理 2 已拒绝
	 * @return
	 */
	@RequestMapping("/myReplenishmentOrder")
	@ResponseBody
	public R myReplenishmentOrder(@RequestParam("userid")Integer userid,@RequestParam("statu")Integer statu) {
		if(userid ==null){
			return R.error("用户id不能为空");
		}else if(statu ==null){
			return R.error("补货单状态不能为空");
		}else{
			JSONArray array = new JSONArray();
			List<ReplenishmentEntity> list = replenishmentService.getReplenishmentByUserId(userid,statu);
			if(list!=null && list.size()>0){
				for(ReplenishmentEntity re:list){
					JSONObject jsonobject = new JSONObject();
					GoodshelvesEntity goodshelves = goodshelvesService.getById(re.getShelvesid());
					jsonobject.put("shelvesname",goodshelves.getShelvesname());
					jsonobject.put("statu",re.getStatu());
					jsonobject.put("time",re.getCreatetime());
					jsonobject.put("id",re.getId());
					jsonobject.put("count",re.getCount());
					List<ProductEntity> lists = replenishmentitemService.getReplenishmentProductById(re.getId());
					if(lists!=null && lists.size()>0){
						jsonobject.put("productimg",lists.get(0).getProductimg());
						jsonobject.put("productname",lists.get(0).getProductname());
					}
					array.add(jsonobject);
				}
			}
			return R.ok().put("orders",array);
		}

		
	}
	

	/**
	 * 订单详情
	 */
	@RequestMapping("/orderdetails")
	@ResponseBody
	public R orderDetails(@RequestParam("userid")Integer userid,@RequestParam("statu")Integer statu) {
		if(userid ==null){
			return R.error("用户id不能为空");
		}else if(statu ==null){
			return R.error("订单状态不能为空");
		}else{
			JSONArray array = new JSONArray();
			List<OrdersEntity> list = ordersService.getOrderList(userid,statu);
			if(list!=null && list.size()>0){
				for(OrdersEntity order:list){
					GoodshelvesEntity goodshelves = goodshelvesService.getById(order.getShelvesid());
					JSONObject jsonobject = new JSONObject();
					jsonobject.put("statu",order.getStatu());
					jsonobject.put("time",order.getCreatetime());
					List<OrdersproductEntity> productlist = ordersService.getOrderProductByOrderNum(order.getOrdernum());
					if(productlist!=null && productlist.size()>0){
						JSONArray jsonarray = new JSONArray();
						for(OrdersproductEntity product:productlist){
							JSONObject json = new JSONObject();
							json.put("productimg",product.getProductimg());
							json.put("productname",product.getProductname());
							json.put("sellmoney",product.getSellMoney());
							json.put("shelvesname",goodshelves.getShelvesname());
							jsonarray.add(json);
						}
						jsonobject.put("products",jsonarray);
					}
					array.add(jsonobject);
				}
			}
			return R.ok().put("orders",array);
		}

	}


	@RequestMapping("/commitOrder")
	@ResponseBody
	public R commitOrder(@RequestParam("userid")Integer userid,@RequestParam("statu")Integer statu) {
		if(userid ==null){
			return R.error("用户id不能为空");
		}else if(statu ==null){
			return R.error("订单状态不能为空");
		}else{

			return R.ok();
		}

	}

}
